﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XDeal.DAL.Entities;
namespace XDeal.DAL.Implements
{
    public class CustomerDAO:BaseDAO
    {
        public CustomerDAO()
            : base()
        { }



        #region GET BASIC
        /// <summary>
        /// Get All Customer
        /// </summary>
        /// <returns>List of Customers</returns>
        public List<Customer> GetAll()
        {
            return XDealDataContext.Customers.ToList<Customer>();
        }

        /// <summary>
        /// Get Customer by ID
        /// </summary>
        /// <param name="ID">Customer ID</param>
        /// <returns>One Customer</returns>
        public Customer GetByID(int ID)
        {
            return XDealDataContext.Customers.Where(ob => ob.ID_i == ID).FirstOrDefault<Customer>();
        }
        #endregion

        #region GET ADVANCE
        //get customer by email
        public List<Customer> GetCustomerByEmail(string email)
        {
            return XDealDataContext.Customers.Where(ob => ob.Email_s == email).ToList<Customer>();
        }

        /// <summary>
        /// Kiem tra dang nhap va tra ve customer
        /// </summary>
        /// <param name="user">email name</param>
        /// <param name="pass">pass da ma hoa</param>
        /// <returns></returns>
        public Customer DoLogIn(string email, string pass)
        {
            return XDealDataContext.Customers.Where(ob => ob.Email_s == email && ob.Password_s == pass).FirstOrDefault<Customer>();
        }

        //get customer by name

        //get customer by birthday
        #endregion

        #region INSERT, UPDATE, DELETE
        /// <summary>
        /// Insert Customer
        /// </summary>
        /// <param name="ob">Customer Object</param>
        /// <returns>True: Successful | False: Fail</returns>
        public bool Insert(Customer ob)
        {
            try
            {
                XDealDataContext.AddToCustomers(ob);
                XDealDataContext.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Update Order by Object
        /// </summary>
        /// <param name="ob">Order Object</param>
        /// <returns>True: Successful | False: Fail</returns>
        public bool Update(Customer ob)
        {
            try
            {
                Customer _ob = GetByID(ob.ID_i);
                if (_ob != null)
                {
                    XDealDataContext.ApplyCurrentValues(_ob.EntityKey.EntitySetName, ob);
                    XDealDataContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Customer by Object
        /// </summary>
        /// <param name="ob">Customer Object</param>
        /// <returns>True: Successful | False: Fail</returns>
        public bool Delete(Customer ob)
        {
            try
            {
                Customer _ob = GetByID(ob.ID_i);
                if (_ob != null)
                {
                    XDealDataContext.DeleteObject(_ob);
                    XDealDataContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Delete By ID
        /// </summary>
        /// <param name="ID">Customer ID</param>
        /// <returns>True: Successful | False: Fail</returns>
        public bool Delete(int ID)
        {
            try
            {
                Customer _ob = GetByID(ID);
                if (_ob != null)
                {
                    XDealDataContext.DeleteObject(_ob);
                    XDealDataContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
